package com.simpou.commons.utils.compiler;

import com.simpou.commons.utils.validation.Assertions;

import java.io.IOException;

import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;


/**
 * Representa um gerenciador de arquivos java.
 *
 * @author Jonas Pereira
 * @since 2011-08-18
 * @version 2012-07-13
 */
final class CompilerFileManager extends ForwardingJavaFileManager<JavaFileManager> {
    /**
     * ClassLoader.
     */
    private CompilerClassLoader classLoader;

    /**
     * Aequivo Java compilado.
     */
    private CompilerJavaFileObject compiledObj;

    /**
     * <p>Constructor for CompilerFileManager.</p>
     *
     * @param fileManager JavaFileManager.
     * @param classLoader AppClassLoader.
     */
    CompilerFileManager(final JavaFileManager fileManager,
        final CompilerClassLoader classLoader) {
        super(fileManager);
        this.classLoader = Assertions.notNull(classLoader);
    }

    /**
     * <p>Constructor for CompilerFileManager.</p>
     *
     * @param fileManager JavaFileManager.
     * @param classLoader AppClassLoader.
     * @param compiledObj Objeto compilado.
     */
    CompilerFileManager(final JavaFileManager fileManager,
        final CompilerClassLoader classLoader,
        final CompilerJavaFileObject compiledObj) {
        this(fileManager, classLoader);
        setObjects(compiledObj);
    }

    /**
     * <p>setObjects.</p>
     *
     * @param compiledObj Arquivo compilado.
     */
    void setObjects(final CompilerJavaFileObject compiledObj) {
        this.compiledObj = Assertions.notNull(compiledObj);
        this.classLoader.setCompiledObj(compiledObj);
    }

    /** {@inheritDoc} */
    @Override
    public JavaFileObject getJavaFileForOutput(final Location location,
        final String qualifiedName, final JavaFileObject.Kind kind,
        final FileObject outputFile) throws IOException {
        return compiledObj;
    }

    /** {@inheritDoc} */
    @Override
    public ClassLoader getClassLoader(final Location location) {
        return classLoader;
    }
}
